package com.webservice.impl;

import com.domain.DataFile;



import com.webservice.FileWebservice;



import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import sun.misc.BASE64Decoder;


import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.io.*;


import java.util.Arrays;
import java.util.Base64;

@WebService(targetNamespace = "http://39.98.59.6/mmms_test/ws/file")
public class FileWebserviceImpl implements FileWebservice {

    /**
     * 上传文件
     * @param  myFile
     * @return 正常返回1，错误返回0
     */
    @Override
    public String uploadFile(@WebParam(name = "DataFile")DataFile myFile) {
        OutputStream os = null;
        String result = "0" ;
        try {
            if (myFile.getPosition() != 0) {
                //根据指定的文件获取一个新的文件输入流：openInputStream(File file),返回FileOutputStream
                os = FileUtils.openOutputStream(
                        new File("D:/mmms-assets/" + myFile.getServerFile()), true);
            } else {
                os = FileUtils.openOutputStream(
                        new File("D:/mmms-assets/" + myFile.getServerFile()), false);
            }
            os.write(myFile.getBytes());
            result = "1";
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(os);
        }
        return result;
    }

    /**
     * 下载文件
     * @param myFile
     * @return
     */
    @Override
    public DataFile downloadFile(@WebParam(name = "DataFile")DataFile myFile) {
        DataFile dataFile = new DataFile();
        InputStream is = null;
        try {
            is = new FileInputStream("D:/mmms-assets/" + myFile.getServerFile());
            is.skip(myFile.getPosition());
            byte[] bytes = new byte[1024 * 1024];
            int size = is.read(bytes);
            if (size > 0) {
                byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);
                myFile.setBytes(fixedBytes);
            } else {
                myFile.setBytes(new byte[0]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
        return myFile;
    }

//    /**
//     * 上传文件2
//     * @param imageString
//     * @param fileName
//     * @return
//     */
//    @WebResult(name = "String")
//    @Override
//    public String uploadImage(@WebParam(name = "imageString")String imageString,
//                              @WebParam(name = "fileName")String fileName) {
//        FileOutputStream fos = null;
//        try{
//            String toDir = "D:/mmms-assets/";   //存储路径
//
//            byte[] buffer = new BASE64Decoder().decodeBuffer(imageString);   //对android传过来的图片字符串进行解码
//            File destDir = new File(toDir);
//            if(!destDir.exists()) destDir.mkdir();
//            fos = new FileOutputStream(new File(destDir, fileName));   //保存图片
//            fos.write(buffer);
//            fos.flush();
//            fos.close();
//            return "上传图片成功!" + "图片路径为：" + toDir;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "上传图片失败！";
//    }

    /**
     * 下载图片2
     * @param fileName
     * @return
     */
    @WebResult(name = "String")
    @Override
    public String downloadImage(@WebParam(name = "fileName")String fileName){
        java.io.File file = new java.io.File("D:/mmms-assets/"+fileName);
        java.io.FileInputStream fis = null;
        try {
            fis = new java.io.FileInputStream("D:/mmms-assets/"+fileName);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 创建保存要上传的图像文件内容的字节数组
        byte[] buffer = new byte[(int) file.length()];
        // 将图像文件的内容读取buffer数组中
        int n = 0;
        try {
            if (fis!=null){
                n = fis.read(buffer);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            if (fis!=null) {
                fis.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String uploadbuffer = Base64.getEncoder().encodeToString(buffer);
        return uploadbuffer;
    }
}
